package cz.muni.fi.pa165.library.web_layer.adminPages.reservation;

import cz.muni.fi.pa165.library.web_layer.adminPages.Reader.ShowAllReader;
import cz.muni.fi.pa165.library.api.ReservationService;
import cz.muni.fi.pa165.library.api.ReservationTO;
import cz.muni.fi.pa165.library.web_layer.ApplicationContextProvider;
import cz.muni.fi.pa165.library.web_layer.HomePage;
import cz.muni.fi.pa165.library.web_layer.Template;
import java.text.SimpleDateFormat;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.AbstractItem;
import org.apache.wicket.markup.repeater.RepeatingView;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.util.string.StringValue;

/**
 *
 * @author dulinka
 */
public class ShowAllReservation extends Template {

    private static final long serialVersionUID = 1L;

    public ShowAllReservation(final PageParameters parameters) {
        super(parameters);

        RepeatingView repeating = new RepeatingView("repeating");
        add(repeating);
        ReservationService reservationService = (ReservationService) ApplicationContextProvider.getApplicationContext().getBean("reservationService");
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        for (ReservationTO reservation : reservationService.findAllReservations()) {
            AbstractItem item = new AbstractItem(repeating.newChildId());
            PageParameters pageParameters = new PageParameters();
            item.add(new BookmarkablePageLink<Void>("delete", ShowAllReservation.class, pageParameters));
            pageParameters.add("reservationid", reservation.getReservationID());
            item.add(new Label("readerTO", reservation.getReaderTO().toString()));
            item.add(new Label("bookTO", reservation.getBookTO().toString()));
            item.add(new Label("reservationDate", dateFormat.format(reservation.getReservationDate())));
            repeating.add(item);
        }

        //delete reservation
        StringValue reservationId = parameters.get("reservationid");
        if (!reservationId.isEmpty()) {
            reservationService.deleteReservation(reservationService.findReservationByID(reservationId.toLong()));
            setResponsePage(ShowAllReservation.class);
        }

        BookmarkablePageLink createLink = new BookmarkablePageLink<>(
                "create", ShowAllReader.class);
        add(createLink);
        
        BookmarkablePageLink homeLink = new BookmarkablePageLink<>(
                "home", HomePage.class);
        add(homeLink);
    }
}
